package com.cuit.zuke.hotel.controller.api;


import com.cuit.zuke.hotel.service.HotelService;
import com.cuit.zuke.hotel.service.HotelSetService;
import com.cuit.zuke.common.exception.GlobalException;
import com.cuit.zuke.common.helper.MD5;
import com.cuit.zuke.hotel.model.hotel.Hotel;
import com.cuit.zuke.hotel.model.hotel.HotelSet;
import com.cuit.zuke.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "酒店管理API接口")
@RestController
@RequestMapping("/hotel/hotelapi")
public class ApiController {

    @Autowired
    private HotelService hotelService;

    @Autowired
    private HotelSetService hotelSetService;

    /**
     * 上传酒店详细信息
     * @param hotel hotel实体
     * @return 1 or 0
     */
    @ApiOperation(value = "上传酒店详细信息")
    @PostMapping("saveHotel")
    public Result saveHospital(@RequestBody Hotel hotel) {

        hotelService.save(hotel);
        return Result.ok();
    }

    /**
     * 获取酒店详细信息
     * @param hotelSet 包括signkey和zkcode的hotelSet实体
     * @return 酒店详细信息
     */
    @ApiOperation(value = "获取酒店详细信息")
    @PostMapping("hotel/show")
    public Result hotel(@RequestBody HotelSet hotelSet) {

        return Result.ok().setData(hotelService.getByZkcode(hotelSet.getZkcode()));
    }

    /**
     * 签名校验
     * @param hotelSet 包括signkey和zkcode的hotelSet实体
     * @return 1 or 0
     */
    @ApiOperation(value = "签名校验")
    @PostMapping("hotel/signKey")
    public Result signkey(@RequestBody HotelSet hotelSet) {
        String hotelSign=hotelSet.getSignKey();
        String zkcode = hotelSet.getZkcode();
        // 进行 MD5 加密
        String signKey = hotelSetService.getSignKey(zkcode);
        String signKeyMD5 = MD5.encrypt(signKey);
        System.out.println(signKeyMD5);
        // 加密后进行比较
        if(!hotelSign.equals(signKeyMD5)){
            throw new GlobalException("密钥签名错误");
        }
        return Result.ok();
    }

}

